﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Windows.Admin.Application.Service;
using Windows.Admin.Domain;
using Windows.Admin.Infrastructure.EFCore;
using Windows.Admin.Infrastructure.EFCore.Extensions;
using Windows.Application.Share;
using Windows.Infrastructure.Share.Extensions;
using Windows.Infrastructure.Share.Security;
using Windows.Infrastructure.Share.Tools;
using Windows.SeedWord;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace Windows.Admin.Application
{
    public class RoleService : IRoleService
    {
        private readonly IMapper _mapper;
        //private readonly ICache _cache;
        private readonly AdminDbContext _db;
        public RoleService(IMapper mapper, AdminDbContext db)
        {
            _mapper = mapper;
            _db = db;
        }
        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        public async Task<List<RoleResponse>> GetRoles()
        {
            var roles = await _db.Role.ToListAsync();
            return _mapper.Map<List<RoleResponse>>(roles);
        }
            /// <summary>
            /// 角色视图
            /// </summary>
            /// <param name="request"></param>
            /// <returns></returns>
            public async Task<PageResponse<RoleResponse>> Query(PageRequest<RoleRequest> request)
        {
            var response = new PageResponse<RoleResponse>();
            IQueryable<Role> query = _db.Role.AsQueryable();
            if (!request.Query.Name.IsNullOrWhiteSpace())
            {
                query = query.Where(x => x.Name.Contains(request.Query.Name!));
            }
            var list = await query.Paging(request.PageIndex, request.PageSize).ToListAsync();
            response.TotalCount = await query.CountAsync();
            response.PageIndex = request.PageIndex;
            response.PageSize = request.PageSize;
            response.List = _mapper.Map<List<RoleResponse>>(list);
            return response;
        }
        /// <summary>
        /// 获取id的角色详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<RoleResponse> Detail(int id)
        {
            var model = await _db.Role.FindByIdAsync(id);
            return _mapper.Map<RoleResponse>(model);
        }
        /// <summary>
        /// 角色保存
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="ApiException"></exception>
        public async Task Save(RoleSaveRequest request)
        {
            if (_db.Role.Count(x => x.Name.ToLower() == request.Name && x.Id != request.Id) > 0)
                throw new ApiException("角色名称已存在!");
            if (request.Id == null)
            {
                Role model = _mapper.Map<Role>(request);
                await _db.Role.AddAsync(model);
            }
            else
            {
                Role? model = await _db.Role.FindByIdAsync(request.Id.Value);
                if (model == null)
                    throw new ApiException("角色不存在!");
                model.Name = request.Name;
                model.Remark = request.Remark;
            }
            await _db.SaveChangesAsync();
        }
    }
}
